package com.edgclub.edg.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.UserApi;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.mato.a.V;


public class SettingMessageActivity extends GzBaseTooolBarActivity implements View.OnClickListener , VolleyListener{

    private ImageView push;
    private RelativeLayout all_people;
    private TextView people_state;
    private User user;
    private UserPrefUtils mUserprefUtils;
    private static final int REQUEST_CODE_MESS = 0;

    private ImageView admin;

    private LinearLayout admin_layout;

    @Override
    public int getContentView() {
        return R.layout.activity_message;
    }

    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("消息通知");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
    }

    @Override
    public void initView() {
        push = (ImageView) findViewById(R.id.push);
        all_people = (RelativeLayout) findViewById(R.id.all_people);
        people_state = (TextView) findViewById(R.id.people_state);
        push.setOnClickListener(this);
        all_people.setOnClickListener(this);
        admin = (ImageView) findViewById(R.id.admin);
        admin.setOnClickListener(this);
        admin_layout = (LinearLayout) findViewById(R.id.admin_layout);
    }

    @Override
    public void initData() {
        mUserprefUtils=new UserPrefUtils(this);
        boolean isReport = mUserprefUtils.isReportTopic();

        if (isReport){
            admin_layout.setVisibility(View.VISIBLE);
        } else {
            admin_layout.setVisibility(View.GONE);
        }
        user=mUserprefUtils.getUser();
        if(mUserprefUtils.getMesssetting()){
            push.setSelected(true);
            all_people.setVisibility(View.VISIBLE);
            if("1".equals(user.getEnable())) {

                if (user.getMsgType().equals("0")){
                    people_state.setText("所有人");
                }
                if (user.getMsgType().equals("1")){
                    people_state.setText("我关注的人");
                }
                if (user.getMsgType().equals("2")){
                    people_state.setText("互相关注的人");
                }
            }
        }else{
            push.setSelected(false);
            all_people.setVisibility(View.GONE);
        }
        if(mUserprefUtils.getAdminMesssetting()){
            admin.setSelected(true);
        }else{
            admin.setSelected(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_back:
                finish();
                break;
            case R.id.push:
                push.setSelected(!push.isSelected());
                mUserprefUtils.saveMesssetting(push.isSelected());
                if (push.isSelected()){
                    all_people.setVisibility(View.VISIBLE);
                } else {
                    all_people.setVisibility(View.GONE);
                }
                String mytType = user.getMsgType();
                setmessage(mytType);
                break;
            case R.id.all_people:
                Intent intent = new Intent(SettingMessageActivity.this,SettingMessageInfoActivity.class);
                intent.putExtra("type", user.getMsgType());
                startActivityForResult(intent, REQUEST_CODE_MESS);
                break;
            case R.id.admin:
                admin.setSelected(!admin.isSelected());
                mUserprefUtils.saveAdminMesssetting(admin.isSelected());
                setAdminmessage();
                break;
        }
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) { //resultCode为回传的标记，我在B中回传的是RESULT_OK
            case RESULT_OK:
                if (requestCode == REQUEST_CODE_MESS){
                    String type = data.getStringExtra("type");
                    setmessage(type);
                }
                break;
            default:
                break;
        }
    }
    public void setmessage(String type) {
        UserParam param = new UserParam();
        param.setUserId(user.getId());
        if (push.isSelected()) {
            param.setEnable("1");
            if (type.equals("0")){
                user.setMsgType("0");
                param.setMsgType("0");
            }else if (type.equals("1")){
                user.setMsgType("1");
                param.setMsgType("1");
            }else{
                user.setMsgType("2");
                param.setMsgType("2");
            }

        }else{
            param.setMsgType("3");
            param.setEnable("0");
        }
        mUserprefUtils.saveUser(user);
        new UserApi(this).personMessage(param,this);
    }

    public void setAdminmessage() {
        UserParam param = new UserParam();
        param.setUserId(user.getId());
        if (push.isSelected()) {
            param.setState("0");
        }else{
            param.setState("1");
        }
        new UserApi(this).reportMessage(param,this);
    }

    @Override
    public void onStart(String tag) {
        showDialog(StringUtils.getString(R.string.get_login), false);
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        if (JSONHelper.isSuccess(response)){
            Toast.makeText(SettingMessageActivity.this,"设置成功",Toast.LENGTH_SHORT).show();

            if (ApiConfig.APIURL.URL_PERSON_MESSAGE.equals(tag)){
                if (user.getMsgType().equals("0")){
                    people_state.setText("所有人");
                }
                if (user.getMsgType().equals("1")){
                    people_state.setText("我关注的人");
                }
                if (user.getMsgType().equals("2")){
                    people_state.setText("互相关注的人");
                }
            }
        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        dismissDialog();
        ResultCode.toastVolleyError(SettingMessageActivity.this, volleyError);
    }
}
